<template>
  <el-dialog :title=" editTab ? '修改' :  '新增'" :close-on-click-modal="false" :visible.sync="visible" width="800px" top="8vh">
    <el-container>
      <el-main style="max-height:60vh;">
         <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
         <!-- v-for="(item,indexOut) in dataForm.dataList" :key="indexOut" -->
          <el-row  class="groupBox">
            <el-col :span="24" :xs="24">
              <el-form-item label="题目类型" prop="itemType" >
            <el-select v-model="dataForm.itemType" :disabled="editTab" @change="(val) => itemTypeChange(val)" style="width: 90%">
              <!-- <el-option v-for="(item,index) in ['私有','公用']" :key="index" :label="item" :value="item">
              </el-option>     -->
               <el-option label="单选" value="1"></el-option> 
               <el-option label="多选" value="2"></el-option>
               <el-option label="问答" value="3"></el-option>                              
            </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24" :xs="24">
              <el-form-item label="题目名称" prop="title" >
            <el-select v-model="dataForm.title" :disabled="editTab"  @change="(val) => itemChange(val)"  placeholder="请选择题目名称"  style="width:90%;">
              <el-option v-for="(itemSub,index) in itemList" :key="index" :label="itemSub.title" :value="itemSub.id"></el-option>
            </el-select>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="12" :xs="24">
              <el-form-item label="分值分数" :prop="'dataList['+index+'].fzFs'" :rules="[
          { required: true, message: '分值分数不能为空', trigger: 'blur' },
          {pattern: /^(0|[1-9]\d?|100)$/, message: '范围在0-100', trigger: 'blur'}
          ]">
                <el-input-number   v-model="item.fzFs" placeholder="分值分数"></el-input-number>
              </el-form-item>
            </el-col> -->
            <el-col :span="12" :xs="24">
              <el-form-item label="排序" prop="orderNo" :rules="[
                { required: true, message: '排序不能为空', trigger: 'blur' }          
                ]">
                <div class="flex justifyContentSpaceBetween alignItemsCenter">
                  <el-input-number :min="1" :max="10000" v-model="dataForm.orderNo"></el-input-number>
                </div>
              </el-form-item>
            </el-col>
            <!-- <div v-if="!editTab">
              <el-button @click="addItem()" v-if="index==dataForm.dataList.length-1" style="float: right;" icon="el-icon-plus">
              </el-button>
            </div> -->
          </el-row>
        </el-form>    
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
/**
 * 绩效考核_测评_评分明细表单
 * @author zgc
 * @date 2021-06-16 10:06:44
 */
import {
 
  getItemByType
} from '@/api/ceping/evalItem'

import {
getdetailObj,
addObj,
putObj
} from '@/api/ceping/jxevalruledetail'

import {
  remote as remoteDict
} from '@/api/admin/dict'
// import {mapGetters} from '_vuex@3.6.2@vuex';
export default {
  data() {
    return {
    
      itemList: [],
      visible: false,
      canSubmit: false,
      editTab: false,
      dataForm: {
        id:0,
        itemId:0,
        orderNo:1,       
        ruleId:0,
          title:'',
        itemType:''
        

      },

      cpPfgzFzOptions: [],
      dataRule: {
        itemId: [{
          required: true,
          message: '题目不能为空',
          trigger: 'blur'
        }],
         title: [{
          required: true,
          message: '题目不能为空',
          trigger: 'blur'
        }],
         orderNo: [{
          required: true,
          message: '所属规则不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  created() {
    remoteDict("cp_pfgz_fz").then(response => {
      this.cpPfgzFzOptions = response.data.data
     //console.log(this.cpPfgzFzOptions)
    })
  },
  // computed: {
  //   ...mapGetters(['permissions'])
  // },
  methods: {
    init(ruleId, id) {
      // this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.editTab = false;
      this.dataForm.ruleId = ruleId;
      this.dataForm.id = id
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
      ////console.log('init->id ',id)
        if (this.dataForm.id) {
          getdetailObj(this.dataForm.id).then(response => {
            
            this.dataForm = response.data.data;
           //console.log('init->response ',this.dataForm)
            this.editTab = true;
          })
        }
         else {
            this.dataForm.title = '';
        }
      })

    },
    itemTypeChange(val){

        this.dataForm.title = ''
        getItemByType(val).then(data=>{
          ////console.log('itemTypeChange->data ',data.data.data)
          // parentItem.itemList = data.data.data;
          this.itemList = data.data.data;
          
        });

    },
    itemChange(val){
       this.dataForm.itemId = val
    },
    // 表单提交
    dataFormSubmit() {
      
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.closeAll();
              this.$notify.success('修改成功');
              this.visible = false;
              this.$emit('getSubList');
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.closeAll();
              this.$notify.success('添加成功');
              this.visible = false;
              this.$emit('getSubList');
              // this.dataForm.dataList = [];
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    }
    //添加数据项
    // addItem() {
    //   this.dataForm.dataList.push({
    //     itemId: this.itemId,
    //     optName: '',
    //     // fzFs: '',
    //     orderNo: '',
    //   });
    // },


  }
}

</script>
